package org.jeecg.modules.music.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.api.vo.user.UserDetailVo;
import org.jeecg.modules.api.vo.user.UserLevelVo;
import org.jeecg.modules.base.service.BaseCommonService;
import org.jeecg.modules.music.entity.MusUser;
import org.jeecg.modules.music.mapper.MusCommentMapper;
import org.jeecg.modules.music.mapper.MusUserMapper;
import org.jeecg.modules.music.service.IMusUserService;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MusUserServiceImpl extends ServiceImpl<MusUserMapper, MusUser> implements IMusUserService {

    @Resource
    private BaseCommonService baseCommonService;


    @Autowired
    private MusUserMapper userMapper;

    @Autowired
    private MusCommentMapper musCommentMapper;

    @Override
    public MusUser getUserByNickName(String nickname) {
        return userMapper.getUserByNickName(nickname);
    }

    @Override
    public MusUser getUserByPhone(String telephone) {
        return baseMapper.getUserByPhone(telephone);
    }

    @Override
    public MusUser getUserByEmail(String email) {
        return userMapper.getUserByEmail(email);
    }

    @Override
    @Transactional
    public void addMusUser(MusUser user) {
        this.save(user);
    }



    /**
     * 校验用户是否有效
     * @param sysUser
     * @return
     */
    @Override
    public Result checkUserIsEffective(MusUser sysUser) {
        Result<?> result = new Result<Object>();
        //情况1：根据用户信息查询，该用户不存在
        if (sysUser == null) {
            result.error500("该用户不存在，请注册");
            baseCommonService.addLog("用户登录失败，用户不存在！", CommonConstant.LOG_TYPE_1, null);
            return result;
        }
        //情况3：根据用户信息查询，该用户已冻结
        if (CommonConstant.USER_FREEZE.equals(sysUser.getStatus())) {
            baseCommonService.addLog("用户登录失败，用户名:" + sysUser.getNickname() + "已冻结！", CommonConstant.LOG_TYPE_1, null);
            result.error500("该用户已冻结");
            return result;
        }
        return result;
    }

    @Override
    public UserDetailVo reqDetail(String uid) {
        return baseMapper.reqDetail(uid);
    }

    @Override
    public UserDetailVo getUserProfileByPhone(String phone) {
        return userMapper.getUserProfileByPhone(phone);
    }

    @Override
    public UserLevelVo queryLevel(String cookie) {
        return userMapper.queryLevel(cookie);
    }

    @Override
    public void editUser(MusUser musUser) {
        this.updateById(musUser);
    }

    @Override
    public void changePassword(String uid, String password) {
        this.userMapper.changePassword(uid,password,new Date());
    }

    @Override
    public void saveMusUser(MusUser user) {
        this.save(user);
    }

    @Override
    public void updateUser(MusUser musUser) {
        this.updateById(musUser);
    }

    @Override
    public void deleteMusUser(String id) {
        this.removeById(id);
    }

    @Override
    public void deleteBatchMusUser(String ids) {
        this.removeByIds(Arrays.asList(ids.split(",")));
    }

    @Override
    public void resetUserPassword(String uid, String password, Date date) {
        baseMapper.resetUserPassword(uid,password,date);
    }

}
